﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using TianYuan.Utils.Extensions;
using TianYuan.Utils.Logging;

namespace DataTest
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlSugarClient db = new SqlSugarClient(
                new ConnectionConfig()
                {
                    ConnectionString = "server=10.190.233.207;port=3306;database=rearservicedb;uid=sccs;password=sccs_db#01",
                    DbType = DbType.MySql,//设置数据库类型
                    IsAutoCloseConnection = true,//自动释放数据务，如果存在事务，在事务结束后释放
                    InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
                });

            //db.Insertable<tb_test>(new tb_test { Id = Guid.NewGuid().ToString(), CreateDate = DateTime.Now, Name = "name1", QueueNum = 1 }).ExecuteCommand();
            //db.Insertable<tb_test>(new tb_test { Id = Guid.NewGuid().ToString(), CreateDate = DateTime.Now, Name = "name2", QueueNum = 2 }).ExecuteCommand();
            //db.Insertable<tb_test>(new tb_test { Id = Guid.NewGuid().ToString(), CreateDate = DateTime.Now, Name = "name3", QueueNum = 3 }).ExecuteCommand();
            
            var testentity = db.Queryable<tb_test>().Where(m => m.QueueNum == 1).First();
            var list = db.Queryable<tb_test>().Where(m => m.QueueNum > 0)
                .Where(m=>m.Id!=testentity.Id)
                .Select<Queue>().OrderBy(m => m.QueueNum).ToList();

            for (int i = 0; i < list.Count(); i++)
            {
                list[i].QueueNum = 5 + i;
            }
            //try
            //{
            //    db.BeginTran();
            //    testentity.QueueNum = 0;
            //    db.Updateable<tb_test>(testentity).ExecuteCommand();
            //    db.Updateable<Queue>(list).UpdateColumns(m => new { m.QueueNum }).ExecuteCommand();
            //    db.CommitTran();
            //}
            //catch (Exception)
            //{
            //    db.RollbackTran();
            //    throw;
            //}
            

        }
    }

    public class Student
    {
        public string Name { set; get; }
    }
    [SugarTable("tb_test")]
    public class tb_test
    {
        /// <summary>
        /// 
        /// <summary>
        [SugarColumn(IsPrimaryKey = true)]
        public string Id { set; get; }
        /// <summary>
        /// 
        /// <summary>
        public string Name { set; get; }
        /// <summary>
        /// 
        /// <summary>
        public DateTime CreateDate { set; get; }
        /// <summary>
        /// 
        /// <summary>
        public DateTime? ModifyDate { set; get; }
        /// <summary>
        /// 
        /// <summary>
        public int QueueNum { set; get; }



    }
    [SugarTable("tb_test")]
    public class Queue
    {
        /// <summary>
        /// 
        /// <summary>
        [SugarColumn(IsPrimaryKey = true)]
        public string Id { set; get; }
        /// <summary>
        /// 
        /// <summary>
        public int QueueNum { set; get; }
    }
}
